#include<bits/stdc++.h>
using namespace std;



const int N=55;
class Solution {
public:
    
    bool check[N][N];
    int n,m;
    int dx[8]={-1,1,0,0,-1,-1,1,1};
    int dy[8]={0,0,-1,1,-1,1,-1,1};
    
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) 
    {
        n=board.size(),m=board[0].size();
         if(board[click[0]][click[1]]=='M')
        {
            board[click[0]][click[1]]='X';
            return board;
        }
        check[click[0]][click[1]]=true;
        board[click[0]][click[1]]='B';
        dfs(board,click[0],click[1]);
        return board;
    }

    void dfs(vector<vector<char>>&board,int i,int j)
    {
        int count=0;
        for(int k=0;k<8;k++)
        {
            int x=dx[k]+i,y=dy[k]+j;
            if(x>=0&&x<n&&y>=0&&y<m&&board[x][y]=='M')
            {
               count++;
            }
        }
        if(count==0) 
        {
             for(int k=0;k<8;k++)
        {
            int x=dx[k]+i,y=dy[k]+j;
            if(x>=0&&x<n&&y>=0&&y<m&&!check[x][y])
            {
              
                board[x][y]='B';
                check[x][y]=true;
                dfs(board,x,y);
            }
        }
        }
        else
        {
            board[i][j] = '0' + count;
        } 
       
    }
};